import React, { Component } from "react";
import Header from "../../components/Header/Header";
import qs from "qs";
export default class List extends Component {
  componentDidMount() {
    let search = this.props.location.search.slice(1); //"id=1&a=2&type=1" --{id:"1",a:"2",type:"1"}
    

    /* 方法1： 原生js
    let arr = search.split("&"); //["id=1","a=2","type=3"]
    let result = {};
    arr.forEach((item) => {
      let tempArr = item.split("="); //["id","1"]
      result[tempArr[0]] = tempArr[1];
    });
    console.log(result);*/

    // 第2种 URLSearchParams
    // let r=new URLSearchParams(search)
    // console.log(r.get("a"))

    // 第3种：qs
    /*
    qs.parse( "id=1&a=2&type=1")  "id=1&a=2&type=1" --{id:"1",a:"2",type:"1"}
    qs.stringify({id:"1",a:"2",type:"1"})  作用：{id:"1",a:"2",type:"1"}--》"id=1&a=2&type=1"
    
    JSON.parse('{id:"1",a:"2",type:"1"}')   作用：'{id:"1",a:"2",type:"1"}'--》{id:"1",a:"2",type:"1"}
    JSON.stringify({id:"1",a:"2",type:"1"}) 作用：{id:"1",a:"2",type:"1"}--》'{id:"1",a:"2",type:"1"}'
   
    */
    let result = qs.parse(search);
    console.log(result);
  }
  render() {
    return (
      <div>
        <Header title="商品列表" back></Header>
      </div>
    );
  }
}
